//
//  WelcomeController.swift
//  WeiBo
//
//  Created by 江潘 on 15/6/28.
//  Copyright © 2015年 江潘. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeController: UIViewController {
    
    var BottomConstraint:NSLayoutConstraint?
    
    override func loadView() {
        
        view = UIView()
        
        let bgIconView = UIImageView(image: UIImage(named: "ad_background"))
        
        view.addSubview(bgIconView)
        
        view.addSubview(iconView)
        
        view.addSubview(msglabel)
        
        bgIconView.translatesAutoresizingMaskIntoConstraints = false
        
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[subview]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview":bgIconView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subview]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview":bgIconView]))
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        BottomConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 100)
        view.addConstraint(BottomConstraint!)
        
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(90)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview":iconView]))
        
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(90)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview":iconView]))
        
        msglabel.translatesAutoresizingMaskIntoConstraints = false
        
        view.addConstraint(NSLayoutConstraint(item: msglabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        
        view.addConstraint(NSLayoutConstraint(item: msglabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
    }

   
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        BottomConstraint?.constant = iconView.frame.origin.y - UIScreen.mainScreen().bounds.height + 100
    
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 6.0, initialSpringVelocity: 10.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    
                    self.msglabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(noticeName, object: true)
                })
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if let urlString = sharedAccount?.avatar_large {
        
            iconView.sd_setImageWithURL(NSURL(string: urlString))
        }
    }
    
 // 懒加载
    lazy var iconView:UIImageView = {
    
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        iconView.layer.cornerRadius = 45
        
        iconView.layer.masksToBounds = true
        
        return iconView
    }()
    
    lazy var msglabel:UILabel = {
    
        let msglabel = UILabel()
        
        msglabel.text = "欢迎回来"
        
        msglabel.alpha = 0
        
        msglabel.font = UIFont.systemFontOfSize(17.0)
        
        msglabel.textColor = UIColor.grayColor()
        
        msglabel.sizeToFit()
        
        return msglabel
    }()
}
